/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  DividerItemDecoration.java   
 * @Package com.jiyi.adapter   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-6-14 上午11:17:33   
 * @version V1.0     
 */
package com.jiyi.adapter;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

/**
 * @ClassName: DividerItemDecoration
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: HYM
 * @date: 2017-6-14 上午11:17:33
 * 
 */
public class DividerItemDecoration extends RecyclerView.ItemDecoration {

	private static final int[] ATTRS = new int[] { android.R.attr.listDivider };

	public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;

	public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

	private Drawable mDivider;

	private int mOrientation;

	public DividerItemDecoration(Context context, int orientation,
			Drawable drvider) {
		// 判断是否有传入自定义的分割线
		if (drvider == null) {
			final TypedArray a = context.obtainStyledAttributes(ATTRS);
			mDivider = a.getDrawable(0);
			a.recycle();
		} else {
			mDivider = drvider;
		}

		setOrientation(orientation);
	}

	public void setOrientation(int orientation) {
		if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
			throw new IllegalArgumentException("invalid orientation");
		}
		mOrientation = orientation;
	}

	@Override
	public void onDraw(Canvas c, RecyclerView parent) {
		Log.v("recyclerview - itemdecoration", "onDraw()");

		if (mOrientation == VERTICAL_LIST) {
			drawVertical(c, parent);
		} else {
			drawHorizontal(c, parent);
		}

	}

	public void drawVertical(Canvas c, RecyclerView parent) {
		final int left = parent.getPaddingLeft();
		final int right = parent.getWidth() - parent.getPaddingRight();

		final int childCount = parent.getChildCount()-1;
		for (int i = 0; i < childCount; i++) {
			final View child = parent.getChildAt(i);
			// if (child.getTag()!=null && child.getTag().equals("head")) {
			// Log.e(child.toString(), " head continue ");
			// continue;
			// }
			RecyclerView v = new RecyclerView(
					parent.getContext());
			final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
					.getLayoutParams();
			final int top = child.getBottom() + params.bottomMargin;
			final int bottom = top + mDivider.getIntrinsicHeight();
			mDivider.setBounds(left, top, right, bottom);
			mDivider.draw(c);
		}
	}

	public void drawHorizontal(Canvas c, RecyclerView parent) {
		final int top = parent.getPaddingTop();
		final int bottom = parent.getHeight() - parent.getPaddingBottom();

		final int childCount = parent.getChildCount();
		for (int i = 0; i < childCount; i++) {
			final View child = parent.getChildAt(i);
			final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
					.getLayoutParams();
			final int left = child.getRight() + params.rightMargin;
			final int right = left + mDivider.getIntrinsicHeight();
			mDivider.setBounds(left, top, right, bottom);
			mDivider.draw(c);
		}
	}

	/**
	 * @author: HYM
	 * @Title: isHeadView
	 * @Description: TODO(判断是否有头部)
	 * @param parent
	 * @return boolean
	 * @date: 2017-6-16
	 */
	boolean isHeadView(RecyclerView parent, int position) {
		final View child = parent.getChildAt(position);
		if (child != null && child.getTag() != null
				&& child.getTag().equals("head")) {
			return true;
		}
		return false;
	}

	@Override
	public void getItemOffsets(Rect outRect, int itemPosition,
			RecyclerView parent) {
		
		int lastCount = parent.getAdapter().getItemCount() - 1;
		// 如果当前条目与是最后一个条目，就不设置divider padding
		if (itemPosition == lastCount) {
			outRect.set(0, 0, 0, 0);
			return;
		}

		if (mOrientation == VERTICAL_LIST) {
			// 如果是头部就不绘制分割线了
			if (!isHeadView(parent, itemPosition)) {
				outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
			}

		} else {
			outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
		}
	}
}
